package cn.gdrj.est.controller;

import cn.gdrj.est.common.controller.BaseController;
import cn.gdrj.est.dto.CommonResult;
import cn.gdrj.est.dto.EvaluateDTO;
import cn.gdrj.est.dto.EvaluateInfoDTO;
import cn.gdrj.est.dto.MalfunctionDTO;
import cn.gdrj.est.entity.Users;
import cn.gdrj.est.service.EvaluateService;
import cn.gdrj.est.service.MalfunctionService;
import cn.gdrj.est.service.TokenService;
import cn.gdrj.est.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品维修估计控制器
 *
 * @author jun
 * @date 2021/11/9
 */
@Api(tags = "商品维修估价API")
@Validated
@RestController
public class EvaluateController extends BaseController {

    @Autowired
    private MalfunctionService malfunctionService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private EvaluateService evaluateService;


    @ApiOperation("根据商品id获取商品评估的维度信息")
    @GetMapping("/api/evaluate/specList")
    public CommonResult<List<MalfunctionDTO>> getSpecList(
            @ApiParam(value = "商品id", required = true)
            @RequestParam @Range(min = 1,message = "无效的商品id值") Long modelId) {

        List<MalfunctionDTO> malfunctionDTOS = malfunctionService.listByModelId(modelId);

        return ResultUtil.success(malfunctionDTOS);
    }

    @ApiOperation("根据用户选择的故障信息进行估价")
    @PostMapping("/api/evaluate/assess")
    public CommonResult<EvaluateDTO> assess(
            @ApiParam(value = "被评估的商品id",required = true) @RequestParam Long modelId,
            @ApiParam(value = "用户选择的故障选项id",required = true) @RequestParam List<Long> optionIds){
        String token = request.getHeader("token");
        Users users = tokenService.getUserInfoByToken(token);

        EvaluateDTO evaluateDTO = evaluateService.assess(modelId, optionIds, users);

        return ResultUtil.success(evaluateDTO);
    }

    @ApiOperation("根据id获取评估的详细信息")
    @GetMapping("/api/evaluate/{id}/assessInfo")
    public CommonResult<EvaluateInfoDTO> assessInfo(
            @ApiParam(value = "评估id",required = true) @PathVariable Long id){

        EvaluateInfoDTO evaluateInfoDTO = evaluateService.getEvaluateInfoById(id);

        return ResultUtil.success(evaluateInfoDTO);
    }
}
